<template>
  <div class="Comment">
    <h5>全部评论({{ comment.total }})</h5>
    <CommentItem :comment="comment" @likeComment="likeComment"></CommentItem>
    <!-- 分页 -->
    <Pagination :pageData="pageData"></Pagination>
  </div>
</template>

<script>
import CommentItem from './CommentItem.vue';
import Pagination from '@/components/pagination/Pagination.vue';

export default {
  props: ['comment', 'pageData', 'id', 'type'],
  data() {
    return {};
  },
  computed: {},
  created() {},
  components: { CommentItem, Pagination },
  methods: {
    //评论
    async postComment(data) {
      //t:1发送，2回复；type:0歌曲，1mv，2歌单，3专辑，4电台，5视频，6动态；id：对应资源id；content：评论内容；commentId：回复的评论id
      let params = {
        t: data.t,
        type: data.type,
        id: data.id,
        content: data.content,
        commentId: data.commentId,
      };
      this.$request('/comment', params);
      //延迟刷新评论，否则无法同步最新评论
      setTimeout(() => {
        this.$emit('refresh');
        this.$message.success('评论成功');
      }, 1500);
    },

    //评论点赞
    async likeComment(cid, liked, index) {
      //id:资源id;cid:评论id;t:1为点赞,0为取消点赞;type：0歌曲，1mv，2歌单，3专辑，4电台，5视频，6动态;/comment/like
      let t;
      if (liked) {
        t = 0;
      } else {
        t = 1;
      }
      let params = {
        id: this.id,
        cid,
        t,
        type: this.type,
      };
      this.$request('/comment/like', params);
      this.comment.comments[index].liked = !this.comment.comments[index].liked;
      if (liked) {
        this.comment.comments[index].likedCount--;
      } else {
        this.comment.comments[index].likedCount++;
      }
    },
  },
};
</script>